const req = require('../../req/index.js');
var app = getApp();
Page({
	data: {
		pageLoading: false,
		appUserInfo: null,
		wxInfo: null,
		hasLogin: false
	},
	onLoad() {
		
	},
	onShow() {
		if (app.globalData.wxInfo) {
			this.setData({
				appUserInfo: app.globalData.userInfo,
				wxInfo: app.globalData.wxInfo,
				hasLogin: true
			})
		}
	},
	quit() {
		/* 
		1.清除登陆状态
		2.清除绑定状态
		3.清除app用户信息
		4.清除微信用户信息
		当前页面更改hasLogin
		*/
	   app.globalData.hasLogin = false;
	   app.globalData.bindingInfoFlag = false;
	   app.globalData.wxInfo = null;
	   app.globalData.userInfo = null;
	   this.setData({
		   hasLogin : false,
		   wxInfo : null,
		   appUserInfo : null
	   })
	},
	yyrecord() {
		if(app.checkBindingInfoFlag()){
			wx.navigateTo({
				url: '../yyrecord/yyrecord'
			});
		}
	},
	registered() {
		if(!app.globalData.hasLogin){
			wx.showModal({
				title: '提示',
				content: '当前用户未登录,请登陆后操作',
				showCancel: false
			})
			return;
		}
		if(!app.globalData.bindingInfoFlag){
			wx.navigateTo({
				url: '../registered/registered'
			});
		}else{
			wx.showModal({
				title: '提示',
				content: '当前用户已绑定手机号码',
				showCancel: false
			})
		}
	},
	modifyinfo() {
		if(app.checkBindingInfoFlag()){
			wx.navigateTo({
				url: '../modifyinfo/modifyinfo'
			});
		}
	},
	getAppUserInfo(e) {
		wx.showLoading({
		  title: '正在登陆...',
		  mask : true
		})
		app.globalData.wxInfo = e.detail.userInfo;
		app.globalData.hasLogin = true;
		this.setData({
			wxInfo: e.detail.userInfo,
			hasLogin: true
		})
		req.user.login()
			.then((res) => {
				let data = res.data;
				if (data.head.code == 1) {
					app.globalData.userInfo = data;
					if (data.sjhm) {
						app.globalData.bindingInfoFlag = true;
					}
				}
				 wx.hideLoading()
			})
			.catch((err)=>{
				this.quit();
				wx.hideLoading()
				req.err.show(err);
			})
	},
	getUserInfo: function(e) {
		console.log(e)
		if(e.detail.errMsg == 'getUserInfo:ok'){
			this.getAppUserInfo(e);
		}
		
	},
})
